package com.cluster.platform.service.impl;

import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.common.api.DictionaryEnums;
import com.cluster.platform.common.api.ResultCode;
import com.cluster.platform.mapper.CmsChannelChildMapper;
import com.cluster.platform.model.CmsChannelChild;
import com.cluster.platform.service.CmsChannelChildService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * @ClassName: CmsChannelChildServiceImpl
 * @Author: xiezhongping
 * @Date: 2021/2/2 16:59
 * @修改日志
 */
@Service
public class CmsChannelChildServiceImpl implements CmsChannelChildService {

    @Autowired
    CmsChannelChildMapper cmsChannelChildMapper;

    @Override
    public CommonResult<Object> searchCmsChannelChildByID(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        CmsChannelChild cmsChannelChild = cmsChannelChildMapper.selectByPrimaryKey(id);
        if (!Optional.ofNullable(cmsChannelChild).isPresent()) {
            return CommonResult.failed(ResultCode.SEARCH_FAIL);
        }
        return CommonResult.success(cmsChannelChild);
    }

    @Override
    public CommonResult<Object> addCmsChannelChild(CmsChannelChild cmsChannelChild) {
        if (!Optional.ofNullable(cmsChannelChild).isPresent()) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        cmsChannelChild.setInsertDate(new Date());
        cmsChannelChild.setStatus(DictionaryEnums.USABLE.getCode());
        int addStatus = cmsChannelChildMapper.insert(cmsChannelChild);
        return addStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.FAILED);
    }

    @Override
    public CommonResult<Object> delCmsChannelChild(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        CmsChannelChild cmsChannelChild = new CmsChannelChild();
        cmsChannelChild.setId(id);
        cmsChannelChild.setStatus(DictionaryEnums.INVALID.getCode());
        int updateStatus = cmsChannelChildMapper.updateByPrimaryKeySelective(cmsChannelChild);
        return updateStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.FAILED);
    }

    @Override
    public CommonResult<Object> deleteCmsChannelChild(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        int delStatus = cmsChannelChildMapper.deleteByPrimaryKey(id);
        return delStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.FAILED);
    }

    @Override
    public CommonResult<Object> updateCmsChannelChild(CmsChannelChild cmsChannelChild) {
        if (!Optional.ofNullable(cmsChannelChild).isPresent()) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        int updateStatus = cmsChannelChildMapper.updateByPrimaryKeySelective(cmsChannelChild);
        return updateStatus > 0 ? CommonResult.success(ResultCode.SUCCESS) : CommonResult.failed(ResultCode.FAILED);
    }

    @Override
    public CommonResult<Object> searchColumnsChild(String parentId, Integer pageNum, Integer pageSize) {
        if (StringUtils.isBlank(parentId) || 0 == pageNum || 0 == pageSize) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<CmsChannelChild> cmsChannelChildren = cmsChannelChildMapper.searchColumnsChild(parentId);
        if (cmsChannelChildren.isEmpty()) {
            return CommonResult.success(ResultCode.NO_RECORD);
        }
        return CommonResult.success(CommonPage.restPage(cmsChannelChildren));
    }
}
